package cn.tx.controller;
import cn.tx.model.User1;
import cn.tx.page.Page;
import cn.tx.service.User1Service;
import cn.tx.util.StringUtil;
import net.sf.json.JSONArray;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import sun.java2d.pipe.SpanShapeRenderer;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping(path = "/user1")
public class User1Controller {

    @Autowired
    private User1Service user1Service;

    /*首页展示*/
    @RequestMapping(path = "/list")
    public ModelAndView list(ModelAndView modelAndView) {
        modelAndView.setViewName("user1/user1_list");
        List<User1> user1List = user1Service.findAll();
        modelAndView.addObject("user1List",user1List);
        //modelAndView.addObject("user1ListJson", JSONArray.fromObject(user1List));
        return modelAndView;
    }


    /*展示年级列表*/
    @RequestMapping(path = "/get_list", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> getList(
            @RequestParam(value="name",required=false,defaultValue="") String name,
            Page page
    ) {
        Map<String,Object> map = new HashMap<String,Object>();
        Map<String,Object> querymap = new HashMap<String,Object>();
        querymap.put("name","%"+name+"%");
        querymap.put("offset", page.getOffset());
        querymap.put("pageSize", page.getRows());
        map.put("rows",user1Service.findList(querymap));
        map.put("total",user1Service.findTotal(querymap));
        return map;
    }



    /*添加user1*/
    @RequestMapping(path = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> add(User1 user1) throws ParseException {
        Map<String, String> map = new HashMap<>();

        if (user1 == null) {
            map.put("type", "error");
            map.put("msg", "添加失败，请联系管理员");
            return map;
        }
        if (StringUtils.isEmpty(user1.getName())) {
            map.put("type", "error");
            map.put("msg", "名字不能为空");
            return map;
        }
        if (StringUtils.isEmpty(user1.getSex())) {
            map.put("type", "error");
            map.put("msg", "性别不能为空");
            return map;
        }
        User1 user1Name = user1Service.findByuser1Name(user1.name);
        if (user1Name!=null){
            map.put("type", "error");
            map.put("msg", "用户名名已存在");
            return map;
        }
        if (user1Service.adduser1(user1) <= 0) {
            map.put("type", "error");
            map.put("msg", "添加失败，请联系管理员");
            return map;
        }
        map.put("type", "success");
        map.put("msg", "添加成功");
        return map;
    }


    /*编辑user1*/
    @RequestMapping(path = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> edit(User1 user1) {
        Map<String, String> map = new HashMap<>();
        User1 userName = user1Service.findByuser1Name(user1.getName());
        if (userName!=null){
            if (user1.getId()!=userName.getId()){
                map.put("type", "error");
                map.put("msg", "id不匹配");
                return map;
            }
        }
        System.out.println(user1);
        if (user1Service.Updateuser1ById(user1)<= 0) {
            map.put("type", "error");
            map.put("msg", "修改失败，请联系管理员");
            return map;
        }
        map.put("type", "success");
        map.put("msg", "修改成功");
        return map;
    }


    /*删除年级*/
    @RequestMapping(path = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> delete(
            @RequestParam(value = "ids[]",required = true) Long[] ids
    ) {
        Map<String, String> map = new HashMap<>();
        if (ids==null||ids.length==0){
            map.put("type", "error");
            map.put("msg", "请至少选择一条数据");
            return map;
        }
        try {
            if(user1Service.delete(StringUtil.joinString(Arrays.asList(ids), ",")) <= 0){
                map.put("type", "error");
                map.put("msg", "删除失败！");
                return map;
            }
        } catch (Exception e) {
            map.put("type", "error");
            map.put("msg", "该年级下存在班级信息，请勿冲动！");
            return map;
        }
        map.put("type", "success");
        map.put("msg", "删除成功");
        return map;
    }

}
